<?php
namespace Common\Models;
use Common\BaseModel;
class CollegeCarousel extends BaseModel
{
    public $res;
    public $key;
    public function getSource()
    {
        return "f_college_carousel";
    }

    public function initialize()
    {
        parent::initialize();
    }


    /**
     * 获取具体的内容
     * @param int $cid 栏目id
     * @param int $cateid 分类id
     * @param int $is_recommend 是否首页推荐 0不需要首页推荐 1首页推荐
     * @param int $type 根据类型获取内容
     * @return mixed
     */
    public function find_list($cid=0,$cateid=0,$is_recommend=0,$type=0){
        $conditions='';
        if($cid){
            if($conditions){
                $conditions .= " AND cid={$cid}";
            }else{
                $conditions .= "cid={$cid}";
            }
        }

        if($cateid){
            if($conditions){
                $conditions .= " AND cateid={$cateid}";
            }else{
                $conditions .= "cateid={$cid}";
            }
        }

        if($is_recommend){
            if($conditions){
                $conditions .= " AND is_recommend={$is_recommend}";
            }else{
                $conditions .= "is_recommend={$is_recommend}";
            }
            $field='id,title,type,cover,created_at,sharecount,zancount,studycount,collectcount';
        }else{
            $field='id,title,cid,cateid,type,cover,created_at,sharecount,zancount,studycount,collectcount';
        }

        if($type){
            if($conditions){
                $conditions .= " AND type={$type}";
            }else{
                $conditions .= "type={$type}";
            }
        }

        $order="sort ASC,id ASC";
        $carousel_list = $this::find(
            array(
                $conditions,
                "order" => $order,
                "columns"=>$field
            )
        );
        return $carousel_list;
    }




    /**
     * 获取具体的内容
     * @param int $cid 栏目id
     * @param int $cateid 分类id
     * @param int $is_recommend 是否首页推荐 0不需要首页推荐 1首页推荐
     * @param int $type 根据类型获取内容
     * @return mixed
     */
    public function get_list($params,$columns="*",$method="find"){

        $page = intval($params['page']);
        $size = intval($params['page_size']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }

        $conditions='';
        if(isset($params['cid']) && !empty($params['cid'])){
            if($conditions){
                $conditions .= " AND cid={$params['cid']}";
            }else{
                $conditions .= "cid={$params['cid']}";
            }
        }
        if(isset($params['type']) && !empty($params['type'])){
            if($conditions){
                $conditions .= " AND type={$params['type']}";
            }else{
                $conditions .= "type={$params['type']}";
            }
        }

        if($method == 'find'){
            $list = $this::find(
                array(
                    $conditions,
                    "columns"=>$columns,
                    "order"   => 'created_at desc',
                    'limit'   => $size,
                    'offset'  => $page
                ))->toarray();
            return $list;
        }else{
            $list = $this->count(
                array(
                    $conditions,
                ));
            return $list;
        }
    }


    /**
     * 根据id获取分类
     */
    public function find_by_id($id){
        $info = $this->findFirstById($id)->toarray();
        if($info){
            return $info;
        }else{
            return [];
        }
    }



}
